//
//  RadioViewController.m
//  SinceLe
//
//  Created by 李元喜 on 15/11/14.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioCell.h"
#import "Macrocs.h"
#import "MusicModel.h"
#import <UIImageView+WebCache.h>
#import "NetWorkHandle.h"
#import "RadioOViewController.h"
@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"RadioCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    [self loadHomeData];
}

//数据解析
-(void)loadHomeData{
    
    NSString *Str = [NSString stringWithFormat:KradioUrl];
    [NetWorkHandle getDataWithURLString:Str compare:^(id object) {
        _dataArray = [NSMutableArray array];
        NSDictionary *dic = object;
        NSArray *arr = dic[@"data"][@"items"];
        for (NSDictionary *a in arr) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:a];
            
            [_dataArray addObject:model];
        }
        
        [_tableView reloadData];
    }];
    
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 400;
    
}

//返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //赋值
    MusicModel *model = _dataArray[indexPath.row];
    
    [cell.Radioimage sd_setImageWithURL:[NSURL URLWithString:model.imageurl ]];
    cell.Radiolabel.text = model.subscribe;
    cell.Radiotitle.text = model.title;
    
    return cell;
    
    
}





//传个网址
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    RadioOViewController *datail = [[RadioOViewController alloc]init];
    MusicModel *model = _dataArray[indexPath.row];
    datail.Wepstr = model.essay_url;
    [self.navigationController pushViewController:datail animated:YES];
    
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
